<?php 
	function LinkDataBase()
	{
		$link = @mysql_connect("localhost","root","root");
		@mysql_query("set names 'UTF8'");
		@mysql_select_db("sqlserver") or die("can not select database");
	}
	
	function DBTableNameGet($UserID, $Year, $Season, $Table_Number)
	{
		if (is_array($Season))
			$Array_Season = $Season;
		else
			$Array_Season = array($Season);
		
		for ($i=0; $i<count($Array_Season); $i++) {
		
			if ($Table_Number!=NULL)
				$sql = "select filename, table_name from sheet where filename LIKE '%".$Year."%' AND 
						filename LIKE '%".$Array_Season[$i]."%' AND table_number = '".$Table_Number."' LIMIT 1";
			else
				$sql = "select filename, table_name from sheet where filename LIKE '%".$Year."%' AND 
						filename LIKE '%".$Array_Season[$i]."%' LIMIT 1";
					
			$result= mysql_query($sql);
			$row = mysql_fetch_assoc($result);
				
			if (!is_array($Season))
				$Array_Temp = array($row['filename'], $row['table_name']);
			else {
				$Array_Temp['filename'][$i] = $row['filename'];
				$Array_Temp['table_name'][$i] = $row['table_name'];
			}
		}
		return $Array_Temp;
	}
	
	function FieldLocationGet($keyword)
	{
		$Temp_array;
		if ($keyword=='Age')
			$Temp_array = array('C','D','E','F');
		else if ($keyword=='Age of Children')
			$Temp_array = array('R','S','T');
		else if ($keyword=='Kids in HH')
			$Temp_array = array('P','Q');
		else if ($keyword=='Ethnicity')
			$Temp_array = array('X','Y');
		else if ($keyword=='Gender')
			$Temp_array = array('A','B');
		else if ($keyword=='Income')
			$Temp_array = array('K','L','M');
		else if ($keyword=='Marital Status')
			$Temp_array = array('N','O');
		else if ($keyword=='Region')
			$Temp_array = array('G','H','I','J');
		else if ($keyword=='Race')
			$Temp_array = array('U','V','W');
		else if ($keyword=='Total')
			$Temp_array = array('total');
		
		return $Temp_array;
	}
	
	function CatchDBdate($Season, $TableName, $Step) {
		
		$Temp_array = array();
		
		if ($Step==1) {
			$PreviousStage_Table = 'sheetreveal_first';
			$NowStage_Table = 'sheetreveal_second';
		}
		else if ($Step==2) {
			$PreviousStage_Table = 'sheetreveal_second';
			$NowStage_Table = 'sheetreveal_third';
		}
		
		//Find sheet contain sub-sheet.
		$sql = "SELECT ".$NowStage_Table.".table_number AS table_number, ".$NowStage_Table.".charttype AS charttype,
				".$NowStage_Table.".keyword AS keyword, ".$NowStage_Table.".contain AS contain, ".$NowStage_Table.".x_axis AS x_axis 
				FROM ".$PreviousStage_Table.", ".$NowStage_Table."
				WHERE ".$PreviousStage_Table.".id = ".$NowStage_Table.".".$PreviousStage_Table."_id AND 
				".$PreviousStage_Table.".table_number='".$TableName."'";
		$result= mysql_query($sql);
		
		if(!$result)
			return;
			
		$numResult = mysql_num_rows($result);
		while($row = mysql_fetch_assoc($result)) {
			
			//Find title of sub question.
			$sub_sql = "select table_name
						FROM sheet 
						WHERE table_number='".$row['table_number']."' AND season='".$Season."'";
			$sub_result= mysql_query($sub_sql);
			
			if(!$sub_result)
				return;
				
			$sub_row = mysql_fetch_assoc($sub_result);
			
			//Multi-item
			if ($numResult>1) {
			
				$sub_table[] = (string)$row['table_number'];
				$sub_charttype[] = (string)$row['charttype'];
				
				$sub_QAname[] = $sub_row['table_name'];
						
				$sub_keyword[] = (string)$row['keyword'];
				$sub_contain[] = (string)$row['contain'];
				$sub_Xaxis[] = (string)$row['x_axis'];
			}
			//Only one.
			else if ($numResult==1) {
				
				$sub_table = (string)$row['table_number'];
				$sub_charttype = (string)$row['charttype'];
				
				$sub_QAname = $sub_row['table_name'];
						
				$sub_keyword = (string)$row['keyword'];
				$sub_contain = (string)$row['contain'];
				$sub_Xaxis = (string)$row['x_axis'];
			}	
		}
		
		if ($numResult!=0) {
			$Temp_array['sub_table'] = $sub_table;
			$Temp_array['sub_charttype'] = $sub_charttype;
			$Temp_array['sub_QAname'] = $sub_QAname;
			$Temp_array['sub_keyword'] = $sub_keyword;
			$Temp_array['sub_contain'] = $sub_contain;
			$Temp_array['sub_Xaxis'] = $sub_Xaxis;
		}
		return $Temp_array;
	}
?>